package com.zhang.redisconfig;

import com.zhang.constant.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class ShopRedisMessageReceiver {
    @Autowired
    private RedisTemplate redisTemplate;
    //接收消息的方法
    public void receiveChannelMessage(String message){
//        log.info("收到了redis发布订阅消息:{}",message);
        //把接受消费者发送过来的消息进行解析并且存储状态位
        if(!StringUtils.isEmpty(message)) {
            //去掉双引号""23:1""
            message = message.replaceAll("\"","");
            String[] splitMessage = message.split(":");
            if (splitMessage == null || splitMessage.length == 2) {
                //splitMessage[0]商品skuId splitMessage[1]商品秒杀状态位
                redisTemplate.opsForValue().set(RedisConst.SECKILL_STATE_PREFIX+splitMessage[0],splitMessage[1]);
            }
        }
    }
}